#include "reg52.h"

sbit R1 = P3^7; 
sbit R2 = P3^6;
sbit R3 = P3^5; 
sbit R4 = P3^4;

sbit H1 = P3^3;
sbit H2 = P3^2;
sbit H3 = P3^1;
sbit H4 = P3^0;

unsigned char ScanKey()
{
unsigned char temp = 0;
	R1 = 0;R2 = 1;R3 = 1;R4 = 1;
	if(H1 == 0) temp = 4;
	if(H2 == 0) temp = 5;
	if(H3 == 0) temp = 6;
	if(H4 == 0) temp = 7;	
	
	R1 = 1;R2 = 0;R3 = 1;R4 = 1;
	if(H1 == 0) temp = 8;
	if(H2 == 0) temp = 9;
	if(H3 == 0) temp = 10;
	if(H4 == 0) temp = 11;	
	
	R1 = 1;R2 = 1;R3 = 0;R4 = 1;
	if(H1 == 0) temp = 12;
	if(H2 == 0) temp = 13;
	if(H3 == 0) temp = 14;
	if(H4 == 0) temp = 15;		
	
	R1 = 1;R2 = 1;R3 = 1;R4 = 0;
	if(H1 == 0) temp = 16;
	if(H2 == 0) temp = 17;
	if(H3 == 0) temp = 18;
	if(H4 == 0) temp = 19;		

return temp; 	
}